<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-group"></i> 数字资产种类
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="search" placeholder="资产名称" class="handle-input mr10" width="55"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        <el-button type="primary" icon="el-icon-lx-add" @click="showBox()">添加数字资产种类
        </el-button>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="name" label="资产名称"></el-table-column>
        <el-table-column prop="sort" label="排序值"></el-table-column>
        <el-table-column prop="instruct" label="包指令"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" :formatter="dateFormat"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" :formatter="dateFormat"></el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="red"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"
          :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>
    <!-- 编辑弹出框 -->
    <el-dialog title="添加节点" v-model="showAddUser" width="60%">
      <el-form ref="form" :model="list" label-width="100px">
        <el-form-item label="资产名称">
          <el-input v-model="list.name"></el-input>
        </el-form-item>
        <el-form-item label="包指令">
          <el-input v-model="list.instruct"></el-input>
        </el-form-item>
        <el-form-item label="排序值">
          <el-input v-model="list.sort"></el-input>
        </el-form-item>
        <el-form-item label="展示图片">
          <el-upload action="" list-type="picture-card" :http-request="uploadFileMethod" :on-preview="picPreview"
            :limit="1" :file-list="picList" :class="{ hide: hideUploadCard }" ref="picClear">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog v-model="dialogVisible">
            <img :src="dialogImageUrl" alt="" class="picPreview" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAddUser = false">取 消</el-button>
          <el-button v-if="isChange == 1" type="primary" @click="append()">确 定</el-button>
          <el-button v-else type="primary" @click="edit()">修改</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  daKindList,
  daKindAdd,
  daKindDel,
  daKindEdit,
} from "../../api/integral";
import { upload } from "../../api/index";
import { formatDate } from "../../common/data";
import moment from "moment";
export default {
  inject: ["reload"],
  filters: {
    formatDate(time) {
      time = time * 1000;
      let date = new Date(time);
      console.log(new Date(time));
      return formatDate(date, "yyyy-MM-dd hh:mm");
    },
  },
  name: "basetable",
  data() {
    return {
      query: {
        pageNo: 1,
      },
      search: "",
      tableData: [],
      pageTotal: 0,
      list: {},
      showAddUser: false,
      isChange: "",
      fileList: "",
      picList: [],
    };
  },
  created() {
    this.getData();
  },
  mounted() { },
  methods: {
    dateFormat: function (row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },

    // 获取分类列表
    getData() {
      daKindList({
        name: this.search,
        limit: 10,
        offset: this.query.pageNo,
      }).then((res) => {
        // console.log(res.data);
        this.tableData = res.data.rows;
        this.pageTotal = res.data.total;
      });
    },

    //多图上传
    uploadFileMethod(param) {
      let formData = new FormData();
      if (param.file) {
        formData.append("file", param.file);
      } else {
        formData.append("file", param);
      }
      upload(formData).then((res) => {
        this.fileList = res.data[0].url;
      });
    },
    // 触发搜索按钮
    handleSearch() {
      this.getData();
    },

    //显示添加
    showBox() {
      this.showAddUser = true;
      this.isChange = 1;
    },

    //新增
    append() {
      this.list.pic = this.fileList;
      daKindAdd(this.list).then((res) => {
        if (res.code === 2000) {
          this.$message.success(res.message);
          this.list = {};
          this.reload();
          this.showAddUser = false;
        } else {
          this.$message.error(res.message);
          this.list = {};
        }
      });
    },
    //显示编辑
    handleEdit(index, row) {
      let data = JSON.parse(JSON.stringify(row));
      this.picList.push({ name: 1, url: data.pic });
      this.list = data;
      this.showAddUser = true;
      this.isChange = 0;
    },
    // 修改
    edit() {
      this.showAddUser = false;
      this.list.pic = this.fileList;
      daKindEdit(this.list).then((res) => {
        if (res.code === 2000) {
          this.$message.success(res.message);
          this.list = {};
          this.reload();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      let data = JSON.parse(JSON.stringify(row));
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          daKindDel({
            id: data.id,
          }).then((res) => {
            if (res.code === 2000) {
              this.$message.success(res.message);
              this.reload();
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => { });
    },
    // 分页导航
    handlePageChange(val) {
      // console.log(val);
      this.query.pageNo = val;
      this.getData();
    },
  },
};
</script>

<style lang='scss' scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.mr10 {
  margin-right: 10px;
}
</style>
